/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.tyut.myspring.myaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;

// 切面
public class QieMian {

    public Object roundTongzhi(ProceedingJoinPoint call) throws Throwable {

        System.out.println("执行前操作...");

        Object o = call.proceed(); //  chain.doFilter( r,r  );
        // 可控制
        System.out.println("后哈哈哈哈.........");

        return o;
    }

    //    通知
    public void qianZhitongzhi() {

        System.out.println("执行操作日志...");
        //        if (System.currentTimeMillis() % 2 == 0) {
        //            throw new RuntimeException("操作失败");
        //        }
    }

    public void fanHuitongzhi(Object retVal) {
        System.out.println("无异常");
        System.out.println("目标对象方法的返回值是: " + retVal);
    }

    public void exceptionTongzhi(Exception ex) {
        System.out.println("出现异常");
        System.out.println("异常信息是: " + ex.getMessage());
    }

    //    不能接受任何参数
    public void afterTongzhi() {
        System.out.println("执行完成");
    }

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        EmpService empService = context.getBean("empService", EmpService.class);
        //        empService.addEmp();
        empService.test();
    }

    public static void main2(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        EmpService empService = context.getBean("empService", EmpService.class);
        //        empService.addEmp();
        empService.delEmp();
    }

    public static void main1(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DeptService deptService = (DeptService) context.getBean("deptService");
        deptService.addDept();
    }
}
